package com.project;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: LinkController
 * @Package: com.project.controller
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/6/23 - 9:06
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("link")
public class LinkController {
    @Autowired
    private ILinkService service;

    @RequestMapping("cutAll")
    public String cutAll(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        CutPageBean<LinkBean> cutBean = service.cutAll(pageNum);
        String str = "<table border='1' width='80%'>";
        str += "<thead><tr><th>姓名</th><th>生日</th><th>电话</th><th>操作</th></tr></thead>";
        str += "<tbody>";

        for (LinkBean link : cutBean.getList()) {
            str += "<tr><td>" + link.getName() + "</td><td>" + link.getPhone() + "</td><td>" +
                    link.getBirthday() + "</td><td><a href='/link/findById?id=" + link.getId() + "'> 修改 </a>" +
                    " <a href='/link/del?id=" + link.getId() + "'> 删除 </a></td></tr>";
        }
        str += "</tbody></table>";
        for (int i = 1; i <= cutBean.getTotalPage(); i++) {
            str += "<a href='/link/cutAll?pageNum=" + i + "'> " + i + " </a>";
        }
        return str;
    }

    @RequestMapping("del")
    public String del(Integer id) {
        service.del(id);
        return cutAll(1);
    }

    @RequestMapping("findById")
    public String findById(Integer id) {
        LinkBean link = service.findById(id);
        String str = "<form action='/link/update'>";
        str += "<input type='hidden' name='id' value='" + id + "'>";
        str += "<p><span>用户名</span><span>" + link.getName() + "</span></p>";
        str += "<p><span>生日</span><span>" + link.getBirthday() + "</span></p>";
        str += "电话：<input type='text' value='" + link.getPhone() + "'name='phone'<br>";
        str += "<input type='submit' value='修改'>";
        str += "</from>";
        return str;
    }

    @RequestMapping("update")
    public String update(int id, String phone) {
        service.update(id, phone);
        return cutAll(1);
    }

    @RequestMapping("add")
    public String add(LinkBean link) {
        service.add(link);
        return "添加成功"+"<a href='/link/cutAll'>查看</a>";
    }

    @RequestMapping("show")
    public List<LinkBean>showLink(){
        List<LinkBean> list=new ArrayList<>();
        list.add(new LinkBean("张勇", LocalDate.parse("1964-03-04"),"13566277144"));
        list.add(new LinkBean("劳尔", LocalDate.parse("1984-03-04"),"13566221144"));
        list.add(new LinkBean("齐达内", LocalDate.parse("1994-03-04"),"13566221144"));
        list.add(new LinkBean("卡卡", LocalDate.parse("1974-03-04"),"13566241144"));
        list.add(new LinkBean("罗纳尔多", LocalDate.parse("1984-03-04"),"13566221154"));
        return list;
    }
}
